package com.xhj.sportsnews.fragment;


import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xhj.sportsnews.BR;
import com.xhj.sportsnews.R;
import com.xhj.sportsnews.adapter.HeadineRecyclerAdapter;
import com.xhj.sportsnews.bean.EventAllEntity;
import com.xhj.sportsnews.callback.HttpService;
import com.xhj.sportsnews.databinding.FragmentEventBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class EventFragment extends Fragment implements View.OnClickListener, PopupWindow.OnDismissListener, RadioGroup.OnCheckedChangeListener, Callback<EventAllEntity> {


    private ArrayAdapter<CharSequence> adapter;
    private TextView textView;
    private PopupWindow popupWindow;
    private ImageView icon_up;
    private ImageView back_down;
    private ObjectAnimator animator;
    private RadioGroup radiogroup;
    private TextView tiltle_tv;
    private HeadineRecyclerAdapter<Object> adapter1;
    private HeadineRecyclerAdapter<Object> adapter2;
    private FragmentEventBinding binding;
    private HashMap<Class<?>, Pair<Integer, Integer>> map;
    private HttpService service;

    public EventFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_event,container,false);
        map = new HashMap<>();
        map.put(EventAllEntity.DataBean.class, Pair.create(R.layout.layout_event_recycler_tv, BR.data));
        map.put(EventAllEntity.DataBean.ContestListBean.ContestBean.class, Pair.create(R.layout.layout_event_recycler_item, BR.contest));
        map.put(EventAllEntity.class,Pair.create(R.layout.layout_event_recycler_tv_2, BR.entity));
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tiltle_tv = ((TextView) view.findViewById(R.id.popupwindow_title));
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.popupwindow_linear);
        back_down = (ImageView) view.findViewById(R.id.back_down);
        animator = (ObjectAnimator) AnimatorInflater.loadAnimator(getContext(), R.animator.anim_rotation);
        animator.setTarget(back_down);
        linearLayout.setOnClickListener(this);
        View contentView = getActivity().getLayoutInflater().inflate(R.layout.layout_popupwindow, null);
        radiogroup = ((RadioGroup) contentView.findViewById(R.id.popupwindow_rg));
        radiogroup.setOnCheckedChangeListener(this);
        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        int xPos = windowManager.getDefaultDisplay().getWidth() / 3;
        popupWindow = new PopupWindow(contentView, xPos, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setOnDismissListener(this);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

        adapter1 = new HeadineRecyclerAdapter<>(getContext(), new ArrayList<>(),map);
        binding.recycler1.setAdapter(adapter1);

        service = new Retrofit.Builder().baseUrl("http://api.lifxapi.com/tiyu/rest/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getEventEntity().enqueue(this);
    }

    @Override
    public void onClick(View v) {
        if (!popupWindow.isShowing()) {
            popupWindow.showAsDropDown(v, -25, -15, Gravity.BOTTOM);
            animator.start();
        }
    }


    @Override
    public void onDismiss() {
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(getContext(), R.animator.anim_rotation_inverse);
        animator.setTarget(back_down);
        animator.start();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        popupWindow.dismiss();
        for (int i = 0; i < group.getChildCount(); i++) {
            if (group.getChildAt(i).getId() == checkedId) {
                RadioButton button = (RadioButton) group.getChildAt(i);
                tiltle_tv.setText(button.getText().toString());
                }
            }
        adapter1.removeAll();
        switch (tiltle_tv.getText()+""){
            case "热门赛事":
                service.getEventHot().enqueue(this);
                break;
            case "全部":
                service.getEventEntity().enqueue(this);
                break;
            case "NBA":
                service.getEventNBA().enqueue(this);
                break;
            case "中国足球":
                service.getEventFootball().enqueue(this);
                break;
            case "国际足球":
                service.getEventInternetFootball().enqueue(this);
                break;
            case "中国篮球":
                service.getEventBasketball().enqueue(this);
                break;
        }
    }



    @Override
    public void onResponse(Call<EventAllEntity> call, Response<EventAllEntity> response) {
        EventAllEntity.DataBean data1 = response.body().getData();
//        binding.setVariable(BR.data,data1);
        List<EventAllEntity.DataBean.ContestListBean.ContestBean> list1 = new ArrayList<>();
        List<EventAllEntity.DataBean.ContestListBean.ContestBean> list2 = new ArrayList<>();
        List<EventAllEntity.DataBean.ContestListBean> contest_list = response.body().getData().getContest_list();
        for (int i = 0; i < contest_list.size(); i++) {
            EventAllEntity.DataBean.ContestListBean.ContestBean contest = contest_list.get(i).getContest();
//            String a = contest.getStart_time().substring(8,10);
//            String b = response.body().getData().getStart_time().substring(3,5);
//            Log.d("1620",a);
//            Log.d("1620",b);
            if (contest.getStart_time().substring(8,10).equals(response.body().getData().getStart_time().substring(3,5))) {
                list1.add(contest);
            }else {
                list2.add(contest);
            }
        }
        adapter1.add(0,data1);
        adapter1.addAll(1,list1);
        adapter1.add(response.body());
        adapter1.addAll(list2);


//        adapter2.addAll(list2);
    }

    @Override
    public void onFailure(Call<EventAllEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

}